package com.example.springcloudeurekaserverproducer.mybatis;

import org.apache.commons.lang.StringUtils;
import sun.applet.Main;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lanyuansong
 * @version V1.0
 * @Title: HandlerCode
 * @Package com.example.springcloudeurekaserverproducer.mybatis
 * @Description: ${todo}(用一句话描述该文件做什么)
 * @date 2018/8/6 9:40
 */
public class HandlerCode {
	private static Pattern humpPattern = Pattern.compile("[A-Z]");
	private static Pattern linePattern = Pattern.compile("_(\\w)");
	/**
	 * 转换数据库的命名 成 java类的骆驼命名法
	 *
	 * @param colName
	 * @return
	 */
	public static String handlerName(String colName) {
		String[] strs = StringUtils.lowerCase(colName).split("_");
		StringBuilder sb = new StringBuilder("");
		if (strs.length > 0) {
			for (int i = 0; i < strs.length; i++) {
				String str = strs[i];
				if (i == 0) {
					str = str.toLowerCase();
				} else {
					str = HandlerCode.upperFirstChar(str);
				}
				sb.append(str);
			}
		}
		return sb.toString();
	}
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	/**
      *
      * 功能描述: 
      *
      * @param:  驼峰转下划线效率慢
      * @return: 
      * @auther: lanyuansong
      * @date: 2018/8/6 9:41
      */
	public static String humpToLine(String str) {
		return str.replaceAll("[A-Z]", "_$0").toLowerCase();
	}
	/**
	 *
	 * 功能描述:
	 *
	 * @param: 驼峰转下划线效率快
	 * @return:
	 * @auther: lanyuansong
	 * @date: 2018/8/6 9:45
	 */
	public static String humpToLine2(String str) {
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}


	public static String upperFirstChar(String str) {
		String newStr = str.substring(0, 1).toUpperCase() + str.substring(1, str.length()).toLowerCase();
		return newStr;
	}

	public static void main(String[] args) {
//		String lineToHump = lineToHump("jf_parent_no_leader");
		String lineToHump="level";
		System.out.println(lineToHump);// platformProjectId
		System.out.println(humpToLine(lineToHump));// platform_project_id
		System.out.println(humpToLine2(lineToHump));// platform_project_id

	}
}
